package 反射;

/**
 * @author shy_black
 * @date 2019/3/21 13:13
 * @Description:
 */
interface Fruit {
    public void eat();
}

class Apple implements Fruit {

    @Override
    public void eat() {
        System.out.println("递过来一个苹果哦。。。");
    }
}

class Pear implements Fruit {

    @Override
    public void eat() {
        System.out.println("递过来一个梨子哦。。。");

    }
}

class FruitFactory {
    public static Fruit getInstance(String fruitName)  {
        Fruit fruit = null;
        Class<?> cls = null;
        try {
            cls = Class.forName(fruitName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            fruit = (Fruit) cls.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return fruit;
    }
}


public class 反射实现工厂模式 {
    public static void main(String[] args)  {
        Fruit fruit = FruitFactory.getInstance("反射.Pear");//包名.类名
        fruit.eat();
    }
}
